/*
   This sketch demonstrates how the SimpleKeypad library works.
   Just change the following code depending on the size of your keypad, the
   symbols on the keys and the pins you use.
*/
// keyboard
#include <SimpleKeypad.h>
#include <stdlib.h>
// U8g2
#include <Arduino.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ PB10, /* data=*/ PB11, /* reset=*/ U8X8_PIN_NONE);

const byte nb_rows = 4;                         // four rows
const byte nb_cols = 4;                         // four columns
char key_chars[nb_rows][nb_cols] = {            // The symbols of the keys
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', 'X'},
  {'C', '0', '=', '/'}
};
byte rowPins[nb_rows] = {PA4, PA5, PA6, PA7};           // The pins where the rows are connected
byte colPins[nb_cols] = {PA0, PA1, PA2, PA3};           // The pins where the columns are connected

SimpleKeypad kp1((char*)key_chars, rowPins, colPins, nb_rows, nb_cols);   // New keypad called kp1
void setup() {
  Serial.begin(115200);
  Serial.println("Press any key on the keypad and it will show up here :");

  // u8g2 初始化
  u8g2.begin();//初始化
  u8g2.enableUTF8Print();   // enable UTF8 support for the Arduino print() function
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setFontDirection(0);
}
void show_u8g2(String formula, String res) {
  u8g2.clearBuffer();
  u8g2.setCursor(0, 15);
  u8g2.print(formula);
  u8g2.setCursor(0, 40);
  u8g2.print(res);
  u8g2.sendBuffer();
}

String formula = "";
String res = "";
float res_value = 0;
byte opreater[10]; // 记录 操作符种类
float opreater_orderlist[10]; // 记录 数字
byte Front_opreater_Pos = 0; //
byte Last_opreater_Pos = 0;
byte opreater_order = 0; // 记录输入位数
byte opreator_index = 0; // 记录 操作符个数
void loop() {
  char key = kp1.getKey();                      // The getKey function scans the keypad every 10 ms and returns a key only one time, when you start pressing it
  if (key) {                                    // If getKey returned any key
    //Serial.println(key);                        // it is printed on the serial monitor
    if (key == 'C') {
      formula = ""; // 全部重置
      opreator_index = 0;
      opreater_order = 0;
      Last_opreater_Pos = 0;
      Front_opreater_Pos = 0;
      res = "";
      res_value = 0;
      for (int i = 0; i < 9; i++) {
        opreater_orderlist[i] = 0;
        opreater[i] = 0;
      }
      show_u8g2(formula, res);
      exit;
    } else {

      formula = formula + key;

      if (key == 'X') {
        if (Last_opreater_Pos == 0) {
          Front_opreater_Pos = Last_opreater_Pos;
        } else
        {
          Front_opreater_Pos = Last_opreater_Pos + 1;
        }

        Last_opreater_Pos = opreater_order;
        opreater[opreator_index] = 2;
        opreater_orderlist[opreator_index] = (formula.substring(Front_opreater_Pos, Last_opreater_Pos)).toInt();
        Serial.println(opreater_orderlist[opreator_index]);
        opreator_index = opreator_index + 1;
      }
      if (key == '+') {
        if (Last_opreater_Pos == 0) {
          Front_opreater_Pos = Last_opreater_Pos;
        } else
        {
          Front_opreater_Pos = Last_opreater_Pos + 1;
        }
        Last_opreater_Pos = opreater_order;
        opreater[opreator_index] = 0;
        opreater_orderlist[opreator_index] = (formula.substring(Front_opreater_Pos, Last_opreater_Pos)).toInt();
        Serial.println(opreater_orderlist[opreator_index]);
        opreator_index = opreator_index + 1;
      }
      if (key == '-') {
        if (Last_opreater_Pos == 0) {
          Front_opreater_Pos = Last_opreater_Pos;
        } else
        {
          Front_opreater_Pos = Last_opreater_Pos + 1;
        }
        Last_opreater_Pos = opreater_order;
        opreater[opreator_index] = 1;
        opreater_orderlist[opreator_index] = (formula.substring(Front_opreater_Pos, Last_opreater_Pos)).toInt();
        Serial.println(opreater_orderlist[opreator_index]);
        opreator_index = opreator_index + 1;
      }
      if (key == '/') {
        if (Last_opreater_Pos == 0) {
          Front_opreater_Pos = Last_opreater_Pos;
        } else
        {
          Front_opreater_Pos = Last_opreater_Pos + 1;
        }
        Last_opreater_Pos = opreater_order;
        opreater[opreator_index] = 3;
        opreater_orderlist[opreator_index] = (formula.substring(Front_opreater_Pos, Last_opreater_Pos)).toInt();
        Serial.println(opreater_orderlist[opreator_index]);
        opreator_index = opreator_index + 1;
      }
      if (key == '=') {
        if (Last_opreater_Pos == 0) {
          Front_opreater_Pos = Last_opreater_Pos;
        } else
        {
          Front_opreater_Pos = Last_opreater_Pos + 1;
        }
        Last_opreater_Pos = opreater_order;
        opreater[opreator_index] = 4;
        opreater_orderlist[opreator_index] = (formula.substring(Front_opreater_Pos, Last_opreater_Pos)).toInt();
        Serial.println(opreater_orderlist[opreator_index]);
        opreator_index = opreator_index + 1;
        ////////// = 开始计算

        for (int i = 0; i < opreator_index; i++) {
          if (opreater[i] == 2) {
            opreater_orderlist[i + 1] = opreater_orderlist[i + 1] * opreater_orderlist[i];
            opreater_orderlist[i] = 0;

            if (i == 0) {
              opreater[i] = 0; // 先默认是 +
            } else {
              opreater[i] = opreater[i - 1];
            }
          }
          if (opreater[i] == 3) {
            opreater_orderlist[i + 1] =opreater_orderlist[i] / opreater_orderlist[i + 1] ;
            opreater_orderlist[i] = 0;

            if (i == 0) {
              opreater[i] = 0; // 先默认是 +
            } else {
              opreater[i] = opreater[i - 1];
            }
          }
        }
        Serial.println("result:");
        res_value = opreater_orderlist[0];
        for (int i = 0; i < opreator_index; i++) {
          Serial.println(opreater_orderlist[i]);
          Serial.println(opreater[i]);
          if (opreater[i] == 0) {
            res_value = res_value + opreater_orderlist[i + 1];
          }
          if (opreater[i] == 1) {
            res_value = res_value - opreater_orderlist[i + 1];
          }
        }

        res = String(res_value);
      }


      show_u8g2(formula, res);

      opreater_order = opreater_order + 1;

    }

  }

}
